/**
 * Created by DukeCrushIt on 2015/8/25.
 */
class BuffMgr{
    public buffs:BuffInfo[];
    public static NO_WAR_BUFF:number = 1097;
    public static SHIELD_BUFF:number = 1098;
    public constructor(){
        this.buffs = [];
    }

    public deleteBuff(petid:number,buffid:number){
        var len = this.buffs.length;
        var buff:BuffInfo;
        var result = 0;
        for(var idx = len-1; idx >= 0; idx--){
            buff = this.buffs[idx];
            if( buff.petId == petid && buff.buffId == buffid){
                this.buffs.splice(idx,1);
                break;
            }
        }
        if( buffid == BuffMgr.NO_WAR_BUFF){
            if( MainScene.getInstance().parent)
                MainScene.getInstance().updateNoWarBuff();
        }else if( buffid == BuffMgr.SHIELD_BUFF){
            if( MainScene.getInstance().parent)
                MainScene.getInstance().updateShield();
        }
    }
    public addBuff(buffInfo:any){
        var len = this.buffs.length;
        var buff:BuffInfo;
        var got:boolean = false;
        for( var idx = 0; idx < len; idx++){
            buff = this.buffs[idx];
            if( buff.buffId == buffInfo.buffId && buff.petId == buffInfo.petId){
                buff.initFromData(buffInfo);
                got = true;
                break;
            }
        }
        if( !got)
            this.buffs.push(new BuffInfo(buffInfo));
    }

    public getHairAddByPetId(petid:number):number{
        var len = this.buffs.length;
        var buff:BuffInfo;
        var result = 0;
        for(var idx = 0; idx < len; idx++){
            buff = this.buffs[idx];
            if( buff.petId == petid && buff.type == Model.TYPE_HAIR){
                result += buff.addition;
            }
        }
        return result;
    }

    public getMoodAddByPetId(petid:number):number{
        var len = this.buffs.length;
        var buff:BuffInfo;
        var result = 0;
        for(var idx = 0; idx < len; idx++){
            buff = this.buffs[idx];
            if( buff.petId == petid && buff.type == Model.TYPE_Mood){
                result += buff.addition;
            }
        }
        return result;
    }

    public getBuffsByPetId(petid:number):BuffInfo[]{
        var arr:BuffInfo[] = [];
        var len = this.buffs.length;
        var buffInfo:BuffInfo;
        for( var idx = 0; idx < len; idx++){
            buffInfo = this.buffs[idx];
            if( buffInfo.petId == petid){
                arr.push(buffInfo);
            }
        }
        return arr;
    }

    public updateBuff(buffInfo){
        var len = this.buffs.length;
        var buff:BuffInfo;
        var got:boolean;
        for(var idx = 0 ; idx < len; idx++){
            buff = this.buffs[idx];
            if( buff.buffId == buffInfo.buffId && buff.petId == buffInfo.petId){
                got = true;
                buff.initFromData(buffInfo);
                break;
            }
        }
        if( !got){
            this.addBuff(buffInfo);
        }
    }

    public getBuffById(buffId):BuffInfo{
        var len = this.buffs.length;
        var buff:BuffInfo;
        var got:boolean;
        for(var idx = 0 ; idx < len; idx++){
            buff = this.buffs[idx];
            if( buff.buffId == buffId){
                return buff;
            }
        }
        return null;
    }

    public getFondleTime(petId):number{
        var len = this.buffs.length;
        var buff:BuffInfo;
        for( var idx = 0; idx < len; idx++){
            buff = this.buffs[idx];
            if( buff.buffId == 1031058 && buff.petId == petId){
                return buff.startTime;
            }
        }
        return 0;
    }
    private static _instance:BuffMgr;
    public static getInstance():BuffMgr{
        if( BuffMgr._instance == null )
            BuffMgr._instance = new BuffMgr();
        return BuffMgr._instance;
    }
}